//+------------------------------------------------------------------+
//|                                              AutoCloseEA_MT4.mq4 |
//|                                     Copyright 2023, YourCompany  |
//|                                             https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, YourCompany"
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict

extern int AutoCloseMinutes = 30;  // 平仓间隔（分钟）

extern bool openMagicNumber = false; // 指定是否开启对应的魔术号

extern int MagicNumber1 = 1234;

extern string MagicNumber2 = "ABCD";

extern int maxProfit1 = 60;  // 最大档倒序默认60

extern int maxProfit2 = 40;  // 最大档倒序默认40

extern int maxProfit3 = 20;  // 最大档倒序默认20

extern int maxProfit4 = 20;  // 最大档倒序默认20

extern int maxProfit5 = 10;  // 最大档倒序默认10

datetime LastCloseTime;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetMillisecondTimer(1000);  // 设置1秒定时器
   LastCloseTime = 0;
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   datetime currentTime = TimeCurrent();

   if(currentTime - LastCloseTime >= AutoCloseMinutes * 60)
     {
      CloseProfitablePositions();
      LastCloseTime = currentTime;
      Print("自动平仓执行时间：", TimeToStr(currentTime));
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool profitCount(double profit)
  {
   if(profit>maxProfit1)
     {
      return true;
     }

   if(profit>maxProfit2)
     {
      return true;
     }

   if(profit>maxProfit3)
     {
      return true;
     }

   if(profit>maxProfit4)
     {
      return true;
     }

   if(profit>maxProfit5)
     {
      return true;
     }
   return false;
  }

//+------------------------------------------------------------------+
//| 平仓盈利持仓函数                                                 |
//+------------------------------------------------------------------+
void CloseProfitablePositions()
  {
   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol())
           {
            double profit = OrderProfit();
            if(profitCount(profit))
              {
               if(openMagicNumber && (OrderMagicNumber() == MagicNumber1 || OrderMagicNumber() == MagicNumber2))
                 {
                  closePosition();
                 }
               else
                 {
                  closePosition();
                 }
              }
           }
        }
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void closePosition()
  {
   double profit = OrderProfit();
   bool result = false;
   if(OrderType() == OP_BUY)
     {
      result = OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
     }
   if(OrderType() == OP_SELL)
     {
      result = OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
     }

   if(result)
     {
      Print("已平仓盈利订单：#", OrderTicket(), " 利润：", profit);
     }
   else
     {
      Print("平仓失败 错误代码：", GetLastError());
     }
  }


//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
